package com.ruoyi.web.controller.qiushi.followup;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.qiushi.domain.followup.QiushiFormItem;
import com.ruoyi.qiushi.mapper.followup.QiushiFormItemMapper;
import com.ruoyi.qiushi.service.followup.IQiushiFormItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 问答单项Controller
 *
 * @author ruoyi
 * @date 2023-12-19
 */
@RestController
@RequestMapping("/qiushi/followup/qiushiformitem")
public class QiushiFormItemController extends BaseController {
    @Autowired
    private IQiushiFormItemService qiushiFormItemService;
    @Autowired
    private QiushiFormItemMapper qiushiFormItemMapper;

    /**
     * 查询问答单项列表
     */
    @PreAuthorize("@ss.hasPermi('qiushi:followup:qiushiformitem:list')")
    @GetMapping("/list")
    public TableDataInfo list(QiushiFormItem qiushiFormItem) {
        startPage();
        List<QiushiFormItem> list = qiushiFormItemService.selectQiushiFormItemList(qiushiFormItem);
        return getDataTable(list);
    }

    /**
     * 导出问答单项列表
     */
    @PreAuthorize("@ss.hasPermi('qiushi:followup:qiushiformitem:export')")
    @Log(title = "问答单项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QiushiFormItem qiushiFormItem) {
        List<QiushiFormItem> list = qiushiFormItemService.selectQiushiFormItemList(qiushiFormItem);
        ExcelUtil<QiushiFormItem> util = new ExcelUtil<QiushiFormItem>(QiushiFormItem.class);
        util.exportExcel(response, list, "问答单项数据");
    }

    /**
     * 获取问答单项详细信息
     */
    @PreAuthorize("@ss.hasPermi('qiushi:followup:qiushiformitem:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(qiushiFormItemService.selectQiushiFormItemById(id));
    }

    /**
     * 新增问答单项
     */
    @PreAuthorize("@ss.hasPermi('qiushi:followup:qiushiformitem:add')")
    @Log(title = "问答单项", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QiushiFormItem qiushiFormItem) {
        qiushiFormItem.setCreateTime(new Date());
        qiushiFormItem.setUpdateTime(new Date());
        return toAjax(qiushiFormItemService.insertQiushiFormItem(qiushiFormItem));
    }

    /**
     * 修改问答单项
     */
    @PreAuthorize("@ss.hasPermi('qiushi:followup:qiushiformitem:edit')")
    @Log(title = "问答单项", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QiushiFormItem qiushiFormItem) {
        qiushiFormItemService.updateQiushiFormItem(qiushiFormItem);

        QiushiFormItem temp = new QiushiFormItem();
        temp.setFormId(qiushiFormItem.getFormId());
        List<QiushiFormItem> list = qiushiFormItemService.selectQiushiFormItemList(temp);
        for (int i = 0; i < list.size(); i++) {

            list.get(i).setSort((long) i + 1);
            list.get(i).setUpdateTime(new Date());

            qiushiFormItemService.updateQiushiFormItem(list.get(i));
        }

        return AjaxResult.success();
    }

    /**
     * 删除问答单项
     */
    @PreAuthorize("@ss.hasPermi('qiushi:followup:qiushiformitem:remove')")
    @Log(title = "问答单项", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(qiushiFormItemService.deleteQiushiFormItemById(id));
    }
}
